//CFG.cpp
//zhunan 2010-3-15
//
#include <iostream>
#include <vector>
#include "crossbit/CFG.h"


using namespace std;
using namespace crossbit;

CFG::CFG()
{
	this->Node_Num=0;
	this->last_node=0;
	this->head==NULL;
	this->p=NULL;
}

CFG::~CFG()
{
}

int CFG::next_block_exist(int path_num, int node_position)
{
	for (int i = 0; i < path_num ; i++)
	{
		if (CFG_Store[last_node].next_block[i] == node_position)
			return i;

	}
	return -1;



}

int CFG::next_block_exist(int current_pos,int path_num,int node_position)
{
	printf("current head pos:	0x%x\n",CFG_Store[current_pos].vb->enterSPC());
	
	if ((CFG_Store[current_pos].path_num==0))
	{
	
		cout<<CFG_Store[CFG_Store[current_pos].next_block[0]==node_position].vb->Get_VBlock_ID()<<endl;
	}
	else
	{

		cout<<CFG_Store[CFG_Store[current_pos].next_block[0]==node_position].vb->Get_VBlock_ID()<<endl;

		cout<<CFG_Store[CFG_Store[current_pos].next_block[1]==node_position].vb->Get_VBlock_ID()<<endl;
	}
	if ((CFG_Store[current_pos].next_block[0]==node_position)||
		(CFG_Store[current_pos].next_block[1]==node_position))
	  return 1;

	return 0;
}



void CFG::Print_CFG()
{
	int i;
	int flag=0;
	cout<<Node_Num<<endl;
//	exit(0);
	for (i=0;i<Node_Num;i++)
	{
		cout<<"Current Block ID:"<<CFG_Store[i].source_id;
		if (CFG_Store[i].path_num==1)
		{
			cout<<"Next ID:	"<<CFG_Store[CFG_Store[i].next_block[0]].source_id<<endl;;
		}
		else
		{
			if (CFG_Store[i].path_num==2)
			{
				cout<<"Next ID:"<<CFG_Store[CFG_Store[i].next_block[0]].source_id<<"	"<<CFG_Store[CFG_Store[i].next_block[1]].source_id<<endl;
			}

		}

	}

}


int CFG::Add_CFG_Node(int Source_id,VBlock * vblock)
{
	if (this->CFG_Store.size()==0)
	{
		head=new CFG_Node;
		head->source_id=Source_id;
		head->path_num=0;
		this->last_node=0;
		head->vb=vblock;
		CFG_Store.push_back(*head);
		this->Node_Num++;
		cout<<"FIRST"<<endl;
		
	}
	else
	{
		p=new CFG_Node;
		p->source_id=Source_id;
		p->path_num=0;
		p->vb=vblock;
			
		CFG_Store.push_back(*p);
		int path_count=0;
		
		if (CFG_Store[last_node].path_num==0)
		{
			path_count=0;
		}
		else
		{
			path_count=1;
		}
		
		CFG_Store[last_node].path_num++;	
		CFG_Store[last_node].next_block[path_count]=CFG_Store.size()-1;
		int tmp=last_node;	
		last_node=CFG_Store.size()-1;
		
		CFG_Store[last_node].next_block[0]=CFG_Store.size();

		CFG_Store[last_node].path_num=0;
		
/*		if (CFG_Store.size()-1==539)
		{
			cout<<"last node:	"<<tmp<<endl;
			cout<<"next:	"<<CFG_Store[539].next_block[0]<<endl;
			exit(0);
		}*/

	



		this->Node_Num++;
		
	}


	return last_node;
}

